package com.wjb.web.backend;


import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ser.std.StdKeySerializers;
import com.wjb.model.EbOrder;
import com.wjb.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import sun.management.jmxremote.ConnectorBootstrap;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    /**
     * 数据放到model中，将页面放到view中
     * 将model渲染到view中
     */
    @Autowired
    private OrderService orderService;

    /**
     * 将数据放到 model中，将页面放到view中
     * spring mvc---将model渲染到view中
     * <p>
     * list ---分页请求数据
     */
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page", defaultValue = "1") int psize,
                       @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {
        System.out.println("start service.....;psize");
        //1.请求service层获取新闻的分页数据
        List<EbOrder> orderList = orderService.listByPage(psize, pageSize);
        int totalPage = orderService.getMaxPage(pageSize);
        //2.传递model(数据)和view视图(jsp)
        model.addAttribute("orderList", orderList);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("curPage", psize);
        return "manage/order";//视图  通过manage/order.jsp进行展示数据
    }

    @RequestMapping("/edit")
    public String edit(Model model, Integer eoId) {
        System.out.println("点击跳转过来了，eoId=" + eoId);
        EbOrder ebOrder = orderService.selectById(eoId);

        model.addAttribute("ebOrder", ebOrder);
        return "manage/order-modify";//manage/order-modify.jsp
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbOrder ebOrder) {
        ebOrder.setEoType(1);
        System.out.println(ebOrder);
        int result = orderService.updateById(ebOrder);
        if (result <= 0) {
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/{id}")
    public String delete(@PathVariable("id")Integer id) {
        int result = orderService.delete(id);
        if (result <= 0) {
        }
        return "manage/manage-result";
        }
    @RequestMapping("/selectBy")
        public String  selectBy(Model model, Integer eoId,String eoUserName,@RequestParam(value = "page", defaultValue = "1") int page,
                                 @RequestParam(value = "pageSize", defaultValue = "3") int pageSize)
        {
             if ((eoId==null)&&(eoUserName.trim().length()==0))
             {
                 return "manage/manage-error";
             }
             else if(eoId==null&&eoUserName.trim().length()!=0)
             {
                   List<EbOrder> orderList = orderService.listByNamePage(page,pageSize,eoUserName);
                 model.addAttribute("orderList", orderList);
                 System.out.println(orderList);
                 return "manage/order";
             }
             else if(eoId!=null)
             {
                   List<EbOrder> orderList = new ArrayList<>();
                   orderList.add(orderService.selectById(eoId));
                 System.out.println(orderList);
                 model.addAttribute("orderList", orderList);
                 return "manage/order";
             }


                 return "manage/manage-error";

        }
    }



